//
//  FTableViewController.m
//  FangXinLang
//
//  Created by AJY on 16/9/19.
//  Copyright © 2016年 AJY. All rights reserved.
//

#import "FTableViewController.h"
#import <AFNetworking.h>
#import "AdModel.h"
#import "DataModel.h"
#import "ScrollModel.h"
#import "STableViewCell.h"
#import "DaTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "SPTableViewCell.h"
#import "WebViewController.h"
#import <MJRefresh.h>
@interface FTableViewController ()
@property(nonatomic,strong)NSMutableArray *adM;
@property(nonatomic,strong)NSMutableArray *daM;
@property(nonatomic,strong)NSMutableArray *scM;
@end

@implementation FTableViewController
-(NSMutableArray *)adM{
    if(!_adM)
    {
        _adM=[NSMutableArray array];
    }
    return _adM;
}
-(NSMutableArray *)daM{
    if(!_daM)
    {
        _daM=[NSMutableArray array];
    }
    return _daM;
}
-(NSMutableArray *)scM{
    if(!_scM)
    {
        _scM=[NSMutableArray array];
    }
    return _scM;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setRefresh];

    [self addData];
    
}
-(void)addData{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:self.url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //[responseObject writeToFile:@"/Users/ajy/Desktop/toutiao.plist" atomically:YES];
        NSDictionary *dict=responseObject[@"data"];
        NSArray *adarr=dict[@"ad"][@"feed"];
            for (NSDictionary *dic in adarr) {
                AdModel *ad=[AdModel admodelWith:dic];
                [self.adM addObject:ad];
            }
            NSArray *scarr=dict[@"focus"];
            for (NSDictionary *dic in scarr) {
                ScrollModel *sc=[ScrollModel scrollmodelWith:dic];
                [self.scM addObject:sc];
            }
                NSArray *daarr=dict[@"feed"];
            for (NSDictionary *dic in daarr) {
                DataModel *da=[DataModel datamodelWith:dic];
                [self.daM addObject:da];
                if(self.adM &&self.daM &&self.scM)
                {
                    [self.tableView reloadData];
                    [self.tableView.mj_header endRefreshing];
                }
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}
-(void)setRefresh{
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewComics)];
    self.tableView.mj_header.automaticallyChangeAlpha=YES;
}
-(void)loadNewComics{
    [self addData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//    return 0;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if([self.name isEqualToString:@"视频"]){
        return self.daM.count+self.adM.count;
    }else if([self.name isEqualToString:@"推荐"]){
       
        return self.daM.count;
    }else{
    return self.daM.count+1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell=nil;
    if([self.name isEqualToString:@"推荐"])
    {
        self.tableView.rowHeight=80;
        static NSString *CELL=@"CELL";
        DaTableViewCell *cellD=[tableView dequeueReusableCellWithIdentifier:CELL];
        if(cellD==nil)
        {
            cellD=[DaTableViewCell datableviewcell];
        }
        if(self.daM.count !=0){
        DataModel *model=self.daM[indexPath.row];
        [cellD.img sd_setImageWithURL:[NSURL URLWithString:model.kpic]];
        cellD.title.text=model.longTitle;
            cellD.sbutitle.text=model.intro;
        }
        cell=cellD;
    }else if ([self.name isEqualToString:@"视频"]){
        static NSString *ID=@"Cell";
        self.tableView.rowHeight=250;
        SPTableViewCell *cellsp=[tableView dequeueReusableCellWithIdentifier:ID];
        if(cellsp==nil)
        {
            cellsp=[SPTableViewCell sptableviewcel];
        }
        if(self.daM.count !=0)
        {
            DataModel *model=self.daM[indexPath.row];
            [cellsp.img sd_setImageWithURL:[NSURL URLWithString:model.kpic]];
            cellsp.title.text=model.longTitle;
            
        }
        cell=cellsp;
    
    }else{
    
    if(indexPath.row==0)
    {
    static NSString *ID=@"cell";
    STableViewCell *cellS= [tableView dequeueReusableCellWithIdentifier:ID];
        self.tableView.rowHeight=200;
if(cellS==nil)
    {
        cellS=[STableViewCell stablecell];

    }
        if(self.scM.count !=0)
    {
        [cellS headWithPicture:self.scM];
    }
        cell=cellS;
    }else{
        
        self.tableView.rowHeight=80;
        static NSString *CELL=@"CELL";
        DaTableViewCell *cellD=[tableView dequeueReusableCellWithIdentifier:CELL];
        if(cellD==nil)
        {
            cellD=[DaTableViewCell datableviewcell];
        }
        DataModel *model=self.daM[indexPath.row-1];
//        if(self.daM.count<=indexPath.row+1){
//            AdModel *model=self.adM[indexPath.row-1-self.daM.count];
//            [cellD.img sd_setImageWithURL:[NSURL URLWithString:model.kpic]];
//            cellD.title.text=model.longTitle;
//            cellD.sbutitle.text=model.intro;
//            cell=cellD;
//        }
        [cellD.img sd_setImageWithURL:[NSURL URLWithString:model.kpic]];
        cellD.title.text=model.longTitle;
        cellD.sbutitle.text=model.intro;
        cell=cellD;
    }
    
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.name isEqualToString:@"视频"]){
        DataModel *model=self.daM[indexPath.row];
        WebViewController *web=[[WebViewController alloc]init];
        web.url=model.url;
        [self.navigationController pushViewController:web animated:YES];
    }
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
